from PIL import Image
import numpy as np

directions = np.array([
    (0, 1),  # 上
    (0, -1),  # 下
    (-1, 0),  # 左
    (1, 0),  # 右
])
face_vertices = np.array([
    # Front face
    (-0.5, -0.5, 0.5, 0.0, 0.0, 1.0),
    (0.5, -0.5, 0.5, 0.0, 0.0, 1.0),
    (0.5, 0.5, 0.5, 0.0, 0.0, 1.0),
    (-0.5, 0.5, 0.5, 0.0, 0.0, 1.0),

    # Back face
    (0.5, -0.5, -0.5, 0.0, 0.0, -1.0),
    (-0.5, -0.5, -0.5, 0.0, 0.0, -1.0),
    (-0.5, 0.5, -0.5, 0.0, 0.0, -1.0),
    (0.5, 0.5, -0.5, 0.0, 0.0, -1.0),

    # Left face
    (-0.5, -0.5, -0.5, -1.0, 0.0, 0.0),
    (-0.5, -0.5, 0.5, -1.0, 0.0, 0.0),
    (-0.5, 0.5, 0.5, -1.0, 0.0, 0.0),
    (-0.5, 0.5, -0.5, -1.0, 0.0, 0.0),

    # Right face
    (0.5, -0.5, 0.5, 1.0, 0.0, 0.0),
    (0.5, -0.5, -0.5, 1.0, 0.0, 0.0),
    (0.5, 0.5, -0.5, 1.0, 0.0, 0.0),
    (0.5, 0.5, 0.5, 1.0, 0.0, 0.0),

    # Top face
    (-0.5, 0.5, 0.5, 0.0, 1.0, 0.0),
    (0.5, 0.5, 0.5, 0.0, 1.0, 0.0),
    (0.5, 0.5, -0.5, 0.0, 1.0, 0.0),
    (-0.5, 0.5, -0.5, 0.0, 1.0, 0.0),

    # Bottom face
    (0.5, -0.5, -0.5, 0.0, -1.0, 0.0),
    (0.5, -0.5, 0.5, 0.0, -1.0, 0.0),
    (-0.5, -0.5, 0.5, 0.0, -1.0, 0.0),
    (-0.5, -0.5, -0.5, 0.0, -1.0, 0.0),
], dtype=np.float32)
def generate_model(image_path):
    vertices = []
    indices = []
    try:
        image = Image.open(image_path).convert("RGBA")  # 确保是 RGBA
        image_array = np.array(image)  # 形状为 (height, width, 4)

        # 提取 Alpha 通道（第四个通道）
        alpha_array = image_array[:, :, 3]  # shape: (height, width)
        width, height = alpha_array.shape[0], alpha_array.shape[1]

        index = 0
        top_start, bottom_start = 4 * 4, 4 * 5

        for x in range(0, width):
            for y in range(0, height):
                if alpha_array[x, y] == 0:
                    continue
                vertices.extend([
                    face_vertices[top_start][0] + x - width / 2,
                    face_vertices[top_start][1],
                    face_vertices[top_start][2] + y - height / 2,
                    face_vertices[top_start][3],
                    face_vertices[top_start][4],
                    face_vertices[top_start][5],

                    face_vertices[top_start + 1][0] + x - width / 2,
                    face_vertices[top_start + 1][1],
                    face_vertices[top_start + 1][2] + y - height / 2,
                    face_vertices[top_start + 1][3],
                    face_vertices[top_start + 1][4],
                    face_vertices[top_start + 1][5],

                    face_vertices[top_start + 2][0] + x - width / 2,
                    face_vertices[top_start + 2][1],
                    face_vertices[top_start + 2][2] + y - height / 2,
                    face_vertices[top_start + 2][3],
                    face_vertices[top_start + 2][4],
                    face_vertices[top_start + 2][5],

                    face_vertices[top_start + 3][0] + x - width / 2,
                    face_vertices[top_start + 3][1],
                    face_vertices[top_start + 3][2] + y - height / 2,
                    face_vertices[top_start + 3][3],
                    face_vertices[top_start + 3][4],
                    face_vertices[top_start + 3][5]
                ])
                indices.extend([index, index + 1, index + 2, index, index + 2, index + 3])
                index += 4
                vertices.extend([
                    face_vertices[bottom_start][0] + x - width / 2,
                    face_vertices[bottom_start][1],
                    face_vertices[bottom_start][2] + y - height / 2,
                    face_vertices[bottom_start][3],
                    face_vertices[bottom_start][4],
                    face_vertices[bottom_start][5],

                    face_vertices[bottom_start + 1][0] + x - width / 2,
                    face_vertices[bottom_start + 1][1],
                    face_vertices[bottom_start + 1][2] + y - height / 2,
                    face_vertices[bottom_start + 1][3],
                    face_vertices[bottom_start + 1][4],
                    face_vertices[bottom_start + 1][5],

                    face_vertices[bottom_start + 2][0] + x - width / 2,
                    face_vertices[bottom_start + 2][1],
                    face_vertices[bottom_start + 2][2] + y - height / 2,
                    face_vertices[bottom_start + 2][3],
                    face_vertices[bottom_start + 2][4],
                    face_vertices[bottom_start + 2][5],

                    face_vertices[bottom_start + 3][0] + x - width / 2,
                    face_vertices[bottom_start + 3][1],
                    face_vertices[bottom_start + 3][2] + y - height / 2,
                    face_vertices[bottom_start + 3][3],
                    face_vertices[bottom_start + 3][4],
                    face_vertices[bottom_start + 3][5]
                ])
                indices.extend([index, index + 1, index + 2, index, index + 2, index + 3])
                index += 4
                # 检查四向决定是否构建面
                for d in range(0, 4):
                    dx, dy = x + directions[d][0], y + directions[d][1]
                    should_generate = dx < 0 or dx >= width or dy < 0 or dy >= height or alpha_array[dx, dy] == 0
                    if should_generate:
                        start = 4 * d
                        vertices.extend([
                            face_vertices[start][0] + x - width / 2,
                            face_vertices[start][1],
                            face_vertices[start][2] + y - height / 2,
                            face_vertices[start][3],
                            face_vertices[start][4],
                            face_vertices[start][5],

                            face_vertices[start + 1][0] + x - width / 2,
                            face_vertices[start + 1][1],
                            face_vertices[start + 1][2] + y - height / 2,
                            face_vertices[start + 1][3],
                            face_vertices[start + 1][4],
                            face_vertices[start + 1][5],

                            face_vertices[start + 2][0] + x - width / 2,
                            face_vertices[start + 2][1],
                            face_vertices[start + 2][2] + y - height / 2,
                            face_vertices[start + 2][3],
                            face_vertices[start + 2][4],
                            face_vertices[start + 2][5],

                            face_vertices[start + 3][0] + x - width / 2,
                            face_vertices[start + 3][1],
                            face_vertices[start + 3][2] + y - height / 2,
                            face_vertices[start + 3][3],
                            face_vertices[start + 3][4],
                            face_vertices[start + 3][5]
                        ])
                        indices.extend([index, index + 1, index + 2, index, index + 2, index + 3])
                        index += 4
    except Exception as e:
        print("Error: Failed to generate model:", e)
        vertices, indices = [], []

    return np.array(vertices, dtype=np.float32), np.array(indices, dtype=np.uint32)

def get_cloud_layout(image_path):
    image = Image.open(image_path).convert("RGBA")  # 确保是 RGBA
    image_array = np.array(image, dtype=np.uint8)  # 形状为 (height, width, 4)

    # 提取 Alpha 通道（第四个通道）
    return image_array[:, :, 3]  # shape: (height, width)

CLOUDS_LAYOUT = get_cloud_layout("Resources/Clouds/clouds.png")